<?php
App::uses('AppController', 'Controller');
/**
 * TicketPrices Controller
 *
 * @property TicketPrice $TicketPrice
 * @property PaginatorComponent $Paginator
 */
class TicketPricesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index($ticket_id = false) {
		$this->loadModel('Ticket');
		if (!$this->Ticket->exists($ticket_id)) {
			throw new NotFoundException(__('Invalid ticket'));
		}
		$this->Ticket->recursive = -1;
		$ticket = $this->Ticket->findById($ticket_id);
		if($ticket['Ticket']['spt_seat']){
			$this->redirect(array('admin'=>true,'plugin'=>'seat','controller'=>'TicketShowing','action'=>'index',$ticket_id));
		}
		$this->TicketPrice->recursive = 0;
		$this->set('ticketPrices', $this->Paginator->paginate('TicketPrice',array('TicketPrice.ticket_id'=>$ticket_id)));
		$this->set('ticket',$ticket);
		
		$ticketPriceStates = $this->TicketPrice->TicketPriceState->find('list');
		$this->set(compact('ticketPriceStates'));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add($ticket_id) {
		$this->loadModel('Ticket');
		if (!$this->Ticket->exists($ticket_id)) {
			throw new NotFoundException(__('Invalid ticket'));
		}else{
			if(strpos($this->referer(),'index')){
	 			$this->Session->write('referer.TPA',$this->referer());
			}
		}
		if ($this->request->is('post')) {
			$this->TicketPrice->create();
			if ($this->TicketPrice->save($this->request->data)) {
				$this->Session->setFlash(__('价格添加成功'),'tip/tip_float',array('t'=>'success'));
				
				$this->Ticket->updateMaxMinPrince($ticket_id);
				if($this->Session->check('referer.TPA')){
					return $this->redirect($this->Session->read('referer.TPA'));
				}else{
					return $this->redirect(array('admin'=>true,'controller'=>'TicketPrices','action' => 'index',$ticket_id));
				}
			} else {
				$this->Session->setFlash(__('价格添加失败，请重试'),'tip/tip_float',array('t'=>'error'));
			}
		}
		
		$this->Ticket->recursive = -1;
		$ticket = $this->Ticket->findById($ticket_id);
		$ticketPriceStates = $this->TicketPrice->TicketPriceState->find('list');
		$this->set('ticket',$ticket);
		$this->set(compact('ticketPriceStates'));
	}
	
	public function admin_batprice($ticket_id = false){
		$data = $this->request->data;
		unset($data['is_repeat']);
		unset($data['repeat']);
		$newdata = $data;
		if(isset($this->request->data['is_repeat']) && is_numeric($this->request->data['repeat'])){
			for($i=0;$i<$this->request->data['repeat'];$i++){
				$plus = '+ '.($i+1). ' day';
				foreach ($data as $tp){
					$tp['TicketPrice']['showtime'] = date('Y-m-d H:i:s',strtotime($tp['TicketPrice']['showtime'].$plus));
					$newdata[] = $tp;
				};
			}
		}
		
		if($this->TicketPrice->saveAll($newdata)){
			$this->loadModel('Ticket');
			$this->Ticket->updateMaxMinPrince($ticket_id);
			$this->set('msg','添加成功');
			$this->set('state',true);
		}else{
			$this->set('msg','操作失败，请重试');
			$this->set('state',false);
		}
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($ticket_id,$id = null) {
		if (!$this->TicketPrice->exists($id)) {
			throw new NotFoundException(__('Invalid ticket price'));
		}else{
			if(strpos($this->referer(),'index')){
	 			$this->Session->write('referer.TPE',$this->referer());
			}
		}
		$this->loadModel('Ticket');
		if (!$this->Ticket->exists($ticket_id)) {
			throw new NotFoundException(__('Invalid ticket'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->TicketPrice->save($this->request->data)) {
				$this->Ticket->updateMaxMinPrince($ticket_id);
				$this->Session->setFlash(__('价格修改成功'),'tip/tip_float',array('t'=>'success'));
				
				if($this->Session->check('referer.TPE')){
					return $this->redirect($this->Session->read('referer.TPE'));
				}else{
					return $this->redirect(array('admin'=>true,'controller'=>'TicketPrices','action' => 'index',$ticket_id));
				}
			} else {
				$this->Session->setFlash(__('价格修改失败，请重试'),'tip/tip_float',array('t'=>'error'));
			}
		} else {
			$options = array('conditions' => array('TicketPrice.' . $this->TicketPrice->primaryKey => $id));
			$this->request->data = $this->TicketPrice->find('first', $options);
		}
		
		$this->Ticket->recursive = -1;
		$ticket = $this->Ticket->findById($ticket_id);
		$ticketPriceStates = $this->TicketPrice->TicketPriceState->find('list');
		$this->set('ticket',$ticket);
		$this->set(compact('ticketPriceStates'));
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($ticket_id) {
		$this->request->onlyAllow('post', 'delete');
		if ($this->TicketPrice->deleteAll(array('TicketPrice.id'=>$this->request->data))) {
			$this->loadModel('Ticket');
			$this->Ticket->updateMaxMinPrince($ticket_id);
			$this->set('state',true);
			$this->set('msg','操作成功');
		} else {
			$this->set('state',false);
			$this->set('msg','操作失败');
		}
	}}
